﻿#include "userform.h"
#include "ui_userform.h"

UserForm::UserForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserForm)
{
    ui->setupUi(this);
    m_pSqlQueryModel = new QSqlQueryModel(this);

    connect(ui->m_btnAdd, &QPushButton::clicked, this, &UserForm::on_add);
    connect(ui->m_btnEdit, &QPushButton::clicked, this, &UserForm::on_edit);
    connect(ui->m_btnDelete, &QPushButton::clicked, this, &UserForm::on_delete);

    connect(ui->m_btnQuery, &QPushButton::clicked, this, &UserForm::init);
}

UserForm::~UserForm()
{
    delete ui;
}

void UserForm::init()
{
    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if(database.isOpen())
    {
        qDebug() << "open";

        int nIndex = ui->m_roleid->currentIndex();
        int nCount = ui->m_roleid->count();
        for(; nCount > 0; nCount--)
        {
            ui->m_roleid->removeItem(nCount - 1);
        }

        ui->m_roleid->addItem(QString::fromLocal8Bit("请选择角色"), -1);

        QSqlQuery sql_query(database);

        QString select_sql = "select role_id, role_name from t_role";
        if(!sql_query.exec(select_sql))
        {
            qDebug() << sql_query.lastError().text();
        }
        else
        {
            while(sql_query.next())
            {
                int roleId = sql_query.value(0).toInt();
                QString roleName = sql_query.value(1).toString();
                ui->m_roleid->addItem(roleName, roleId);
            }
            if(nIndex >= 0)
            {
                ui->m_roleid->setCurrentIndex(nIndex);
            }
        }

        QString sql = QString("select u.user_id, u.user_name, u.role_id, r.role_name "
                              "from t_user u left join t_role r on u.role_id = r.role_id "
                              "where u.user_name like '%%1%'")
                .arg(ui->m_userName->text());

        if(ui->m_roleid->currentData().toLongLong() != -1)
        {
            sql = QString(sql + " and u.role_id = %1")
                    .arg(ui->m_roleid->currentData().toLongLong());
        }

        qDebug() << sql;

        m_pSqlQueryModel->setQuery(sql, database);
        m_pSqlQueryModel->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit("主键"));
        m_pSqlQueryModel->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit("用户名"));
        m_pSqlQueryModel->setHeaderData(2, Qt::Horizontal, QString::fromLocal8Bit("角色ID"));
        m_pSqlQueryModel->setHeaderData(3, Qt::Horizontal, QString::fromLocal8Bit("角色名"));

        ui->m_tableViewUser->setModel(m_pSqlQueryModel);
        ui->m_tableViewUser->setColumnHidden(2, true);

    }
    else
    {
        qDebug() << "open fail";
    }
}

void UserForm::on_add()
{
    UserDialog userDialog;
    User user;
    user.op = "ADD";
    userDialog.init(user);
    if(userDialog.exec() == QDialog::Accepted)
    {
        init();
    }
}

void UserForm::on_edit()
{
    User user;
    user.op = "EDIT";

    int row= ui->m_tableViewUser->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::information(this,
                                 QString::fromLocal8Bit("提示"),
                                 QString::fromLocal8Bit("请选择修改项"));
        return;
    }

    QAbstractItemModel *model = ui->m_tableViewUser->model();
    QModelIndex index = model->index(row, 0);//选中行第一列的内容
    QVariant data = model->data(index);
    user.userId = data.toLongLong();
    index = model->index(row, 1);
    data = model->data(index);
    user.userName = data.toString();
    index = model->index(row, 2);
    data = model->data(index);
    user.roleId = data.toLongLong();

    UserDialog userDialog;
    userDialog.init(user);
    if(userDialog.exec() == QDialog::Accepted)
    {
        init();
    }
}

void UserForm::on_delete()
{
    int row= ui->m_tableViewUser->currentIndex().row();
    if(row < 0)
    {
        QMessageBox::information(this,
                                 QString::fromLocal8Bit("提示"),
                                 QString::fromLocal8Bit("请选择删除项"));
        return;
    }

    if(QMessageBox::question(this,
                             QString::fromLocal8Bit("提示"),
                             QString::fromLocal8Bit("请确认删除"),
                             QString::fromLocal8Bit("取消"),
                             QString::fromLocal8Bit("确认")) != 1)
    {
        return;
    }

    QAbstractItemModel *model = ui->m_tableViewUser->model();
    QModelIndex index = model->index(row,0);//选中行第一列的内容
    QVariant data = model->data(index);

    QString sql = QString("delete from t_user where user_id = %1")
            .arg(data.toLongLong());
    QSqlDatabase database = QSqlDatabase::database(DbUtil::SQLITE_CONNECTION_NAME, true);
    if (database.open())
    {
        QSqlQuery sql_query(database);
        if(sql_query.exec(sql))
        {
            init();
            qDebug() << "delete t_user " << sql;
        }
    }
}
